package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;


import com.weimei.conversionTreasure.http.Repository;

import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class TitleVM extends BaseViewModel<Repository> {

    //标题文字
    public ObservableField<String> titleText = new ObservableField<>("");
    //右边文字
    public ObservableField<String> rightText = new ObservableField<>("");
    //右边文字带下拉
    public ObservableField<String> rightTextXl = new ObservableField<>("更多");

    //右边文字带下拉
    public ObservableField<String> noMsg = new ObservableField<>("暂无数据");

    //右边文字的观察者
    public ObservableInt rightTextVisibleObservable = new ObservableInt(View.GONE);

    //右边文字的观察者带下拉
    public ObservableInt rightTextVisibleObservableXl = new ObservableInt(View.GONE);

    //右边图标的观察者
    public ObservableInt rightIconVisibleObservable = new ObservableInt(View.GONE);


    //空布局的观察者
    public ObservableInt emptyVisibleObservable = new ObservableInt(View.GONE);

    //兼容databinding，去泛型化
    public TitleVM titleVM;

    public Context mContext;

    public TitleVM(@NonNull Application application) {
        this(application,null);
    }

    public TitleVM(@NonNull Application application, BaseModel model) {
        super(application, (Repository) model);
        titleVM = this;
    }

    public void setContext(Activity activity) {
        mContext = activity;
    }

    /**
     * 设置标题
     *
     * @param text 标题文字
     */
    public void setTitleText(String text) {
        titleText.set(text);
    }


    /**
     * 设置右边文字
     *
     * @param text 右边文字
     */
    public void setRightText(String text) {
        rightText.set(text);
        setRightTextVisible(View.VISIBLE);
    }

    /**
     * 设置空白描述
     * @param text
     */
    public void setNoDataText(String text){
        noMsg.set(text);
        showNoDataView(true);
    }

    /**
     * 设置右边文字
     *
     * @param text 右边文字
     */
    public void setRightTextXl(String text) {
        rightTextXl.set(text);
        setRightTextVisibleXl(View.VISIBLE);
    }

    /**
     * 设置右边文字的显示和隐藏
     *
     * @param visibility
     */
    public void setRightTextVisible(int visibility) {
        rightTextVisibleObservable.set(visibility);
    }

    /**
     * 设置空布局是否显示
     * @param show 显示
     */
    public void showNoDataView(boolean show){
        if (show){
            emptyVisibleObservable.set(View.VISIBLE);
        }else {
            emptyVisibleObservable.set(View.GONE);
        }
    }

    /**
     * 设置右边文字的显示和隐藏
     *
     * @param visibility
     */
    public void setRightTextVisibleXl(int visibility) {
        rightTextVisibleObservableXl.set(visibility);
    }

    /**
     * 设置右边图标的显示和隐藏
     *
     * @param visibility
     */
    public void setRightIconVisible(int visibility) {
        rightIconVisibleObservable.set(visibility);
    }

    /**
     * 返回按钮的点击事件
     */
    public final BindingCommand backOnClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            backOnClick();
            finish();
        }
    });

    public BindingCommand rightTextOnClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            rightTextOnClick(rightText.get());
        }
    });


    public BindingCommand rightTextOnClickXl = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            rightTextOnClickXl();
        }
    });

    public BindingCommand rightIconOnClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            rightIconOnClick();
        }
    });

    /**
     * 右边文字的点击事件，子类可重写
     * @param s
     */
    protected void rightTextOnClick(String s) {
    }

    /**
     * 右边文字的点击事件，子类可重写
     */
    protected void rightTextOnClickXl() {
    }

    /**
     * 右边图标的点击事件，子类可重写
     */
    protected void rightIconOnClick() {
    }

    /**
     * 右边文字的点击事件，子类可重写
     */
    protected void backOnClick() {
    }
}